package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.hr.dto.EmpWorkAddDTO;
import org.jsola.hr.dto.EmpWorkUpdateDTO;
import org.jsola.hr.entity.EmpWorkDO;
import org.jsola.hr.query.EmpWorkQuery;
import org.jsola.hr.service.IEmpWorkService;
import org.jsola.hr.vo.EmpWorkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * @author zhr
 */
@Component
public class EmpWorkTestHelper extends BaseTestHelper {
    
    @Autowired
    private IEmpWorkService empWorkService;

    /**
     * 生成add测试数据
     * @return EmpWorkAddDTO
     */
    public EmpWorkAddDTO getEmpWorkAddDTO(){
        EmpWorkAddDTO empWorkAddDTO = new EmpWorkAddDTO();
        empWorkAddDTO.setEmpId(getRandomValue("varchar",32));
        empWorkAddDTO.setCompany(getRandomValue("varchar",255));
        empWorkAddDTO.setPosition(getRandomValue("varchar",255));
        empWorkAddDTO.setStartTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        empWorkAddDTO.setEndTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        empWorkAddDTO.setLeaveReason(getRandomValue("varchar",255));
        empWorkAddDTO.setWitName(getRandomValue("varchar",255));
        empWorkAddDTO.setWitPhone(getRandomValue("varchar",255));
        empWorkAddDTO.setCompanyId(getRandomValue("varchar",32));
        return empWorkAddDTO;
    }


    /**
     * 生成update测试数据
     * @return EmpWorkUpdateDTO
     */
    public EmpWorkUpdateDTO getEmpWorkUpdateDTO(EmpWorkVO empWorkVO){
        return empWorkService.selectDOById(empWorkVO.getId(), SITE_ID).to(EmpWorkUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return EmpWorkQuery
     */
    public EmpWorkQuery getEmpWorkQuery() {
        EmpWorkQuery empWorkQuery = new EmpWorkQuery();
        List<EmpWorkDO> empWorkDOList = empWorkService.list(new EmpWorkDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(empWorkDOList)) {
            EmpWorkDO empWorkDO = empWorkDOList.get(0);
            empWorkQuery = empWorkDO.to(EmpWorkQuery.class);
            if (empWorkDO.getStartTime() != null) {
                empWorkQuery.setStartTimeStart(DateKit.getBeforeDay(empWorkDO.getStartTime(),-1));
                empWorkQuery.setStartTimeEnd(DateKit.getBeforeDay(empWorkDO.getStartTime(),1));
            }
            if (empWorkDO.getEndTime() != null) {
                empWorkQuery.setEndTimeStart(DateKit.getBeforeDay(empWorkDO.getEndTime(),-1));
                empWorkQuery.setEndTimeEnd(DateKit.getBeforeDay(empWorkDO.getEndTime(),1));
            }
            if (empWorkDO.getGmtCreate() != null) {
                empWorkQuery.setGmtCreateStart(DateKit.getBeforeDay(empWorkDO.getGmtCreate(),-1));
                empWorkQuery.setGmtCreateEnd(DateKit.getBeforeDay(empWorkDO.getGmtCreate(),1));
            }
            if (empWorkDO.getGmtModified() != null) {
                empWorkQuery.setGmtModifiedStart(DateKit.getBeforeDay(empWorkDO.getGmtModified(),-1));
                empWorkQuery.setGmtModifiedEnd(DateKit.getBeforeDay(empWorkDO.getGmtModified(),1));
            }
        }
        return empWorkQuery;
    }

    /**
     * 保存示例
     * @return EmpWorkVO
     */
    public EmpWorkVO saveEmpWorkExample(){
        EmpWorkAddDTO addDTO = this.getEmpWorkAddDTO();
        EmpWorkVO empWorkVO = empWorkService.save(addDTO,buildTokenUser());
        return empWorkVO;
    }


}

